$(function () {
    getInit();
    $(".dealCheck").click(function () {
        getInit();
    });
});

function getInit() {
    var settleCheck = null;
    var refundCheck = null;
    var arrearsCheck = null;
    var deductionCheck = null;
    if ($("#settleCheck").is(':checked')) {
        settleCheck = "1";
    } else {
        settleCheck = "0";
    }
    if ($("#refundCheck").is(':checked')) {
        refundCheck = "1";
    } else {
        refundCheck = "0";
    }
    if ($("#arrearsCheck").is(':checked')) {
        arrearsCheck = "1";
    } else {
        arrearsCheck = "0";
    }
    if ($("#deductionCheck").is(':checked')) {
        deductionCheck = "1";
    } else {
        deductionCheck = "0";
    }
    var reservationsId = $("#reservationsId").val();
    $.ajax({
        url: Feng.ctxPath + "/consultant/selectDealInfoByCheck",
        type: "post",
        data: {
            "settleCheck": settleCheck,
            "refundCheck": refundCheck,
            "arrearsCheck": arrearsCheck,
            "deductionCheck": deductionCheck,
            "reservationsId": reservationsId
        },
        success: function (data) {
            var html = '';
            var orderList = data.list;
            for (var i = 0; i < orderList.length; i++) {
                var order = orderList[i];
                var realAmount = parseFloat(order.serviceActualPay) + parseFloat(order.depositAmount);
                html += '<div class="order">' +
                    '<div style="display: flex;text-align: center;border-bottom: 1px solid #DDDDDD;padding: 5px 0">' +
                    '<div style="flex: 1;" class="showDetail">' +
                    ' <span class="glyphicon glyphicon-collapse-down"></span>' +
                    ' <span class="glyphicon glyphicon-collapse-up" style="display: none"></span>' +
                    '</div>' +
                    '<div style="flex: 1;">' + order.outpatientName + '</div>' +
                    '<div style="flex: 1;">' + order.documentNumber + '</div>' +
                    '<div style="flex: 2;">' + order.operationTime + '</div>' +
                    '<div style="flex: 1;">' + order.dealStatus + '</div>' +
                    '<div style="flex: 1;">' + order.doctor + '</div>' +
                    '<div style="flex: 1;">' + order.nurse + '</div>' +
                    '<div style="flex: 1;">' + order.tcName + '</div>' +
                    '<div style="flex: 1;">' + order.foldingPay + '</div>' +
                    '<div style="flex: 1;">' + realAmount + '</div>' +
                    '<div style="flex: 1;">' + order.deductionAmount + '</div>' +
                    '<div style="flex: 1;">' + order.arrears + '</div>' +
                    '<div style="flex: 2;">' + order.remarks + '</div>' +
                    '<div style="flex: 2.5;">' +
                    '<span><a href="javascript:;" onclick="detailsPrint(\'' + order.operationTime + '\',\'' + order.outpatientName + '\')">详情打印</a></span>' +
                    '</div>' +
                    '</div>' +
                    '<div style="padding: 15px 20px;overflow: hidden;display: none">' +
                    '<div class="dealInfoList" style="box-shadow: 0 0 8px #cccccc;border-radius: 6px;overflow: hidden;">' +
                    '<div style="display: flex;padding: 4px 0;background: #fec182;color: #666a73;text-align: center">' +
                    '<div style="flex: 1;">操作诊所</div>' +
                    '<div style="flex: 2;">交易时间</div>' +
                    '<div style="flex: 1;">交易类型</div>' +
                    '<div style="flex: 1;">交易状态</div>' +
                    '<div style="flex: 1;">收费人</div>' +
                    '<div style="flex: 1;">医生</div>' +
                    '<div style="flex: 1;">护士</div>' +
                    '<div style="flex: 1;">实收金额</div>' +
                    '<div style="flex: 1;">优惠抵扣</div>' +
                    '<div style="flex: 1.5;">收费方式</div>' +
                    '<div style="flex: 1;">折扣</div>' +
                    '<div style="flex: 2;">备注</div>' +
                    '<div style="flex: 2.5;">操作</div>' +
                    '</div>';
                var dealList = order.dealList;
                if (dealList.length > 0) {
                    for (var j = 0; j < dealList.length; j++) {
                        var deal = dealList[j];
                        html += '<div class="openOrder" style="display: flex;padding: 7px 0;border-top: 1px solid #eeeeee;text-align: center;">' +
                            '<div style="flex: 1;">' + deal.outpatientName + '</div>' +
                            '<div style="flex: 2;">' + deal.getTime + '</div>';
                        if (deal.isDo == '3') {
                            html += '<div class="dealType" style="flex: 1;">还欠款</div>';
                        } else if (deal.isDo == '4') {
                            html += '<div class="dealType" style="flex: 1;">退款</div>';
                        } else if (deal.isDo == '5') {
                            html += '<div class="dealType" style="flex: 1;">结账</div>';
                        }
                        html += '<div style="flex: 1;">' + deal.dealStatus + '</div>' +
                            '<div style="flex: 1;">' + deal.rechargePeople + '</div>' +
                            '<div style="flex: 1;">' + deal.doctor + '</div>' +
                            '<div style="flex: 1;">' + deal.nurse + '</div>' +
                            '<div style="flex: 1;">' + deal.serviceActualPay + '元</div>' +
                            '<div style="flex: 1;">' + deal.deductionAmount + '</div>' +
                            '<div style="flex: 1.5;">(' + deal.checkoutMethod + ')</div>' +
                            '<div style="flex: 1;">' + deal.orderDiscount + '</div>' +
                            '<div style="flex: 2;">' + deal.remarks + '</div>' +
                            '<div style="flex: 2.5;">\n' +
                            '<span><a onclick="printingDeal(\'' + order.operationTime + '\',\'' + order.outpatientName + '\',\'' + deal.getTime + '\',\'' + reservationsId + '\')">详情打印</a></span>' +
                            '</div></div>';
                    }
                }
                html += '</div></div>';
            }
            $("#orderhtml").html(html);
            $(".chargeBtn > button").eq(0).css("background", "#dddddd")
            $(".chargeBtn > button").off('click').on('click', function () {
                var index = $(this).index();
                $(this).css("background", "#dddddd").siblings().css("background", "white");
                $(".chargeContent > div").eq(index).show().siblings().hide()
            });
            $(".chargeContent").on('click', '.showDetail', function () { //成交记录下拉箭头
                $(this).parent().siblings().toggle();
                $(this).children().toggle();
            });
        }
    })
}

function selectAll() {
    if ($("#checkall").prop("checked")) {
        $("input[type='checkbox'][name='checkedres']").prop("checked", true);//全选
        chooseArrearsDeal()
    } else {
        $("input[type='checkbox'][name='checkedres']").prop("checked", false);  //取消全选
        chooseArrearsDeal()
    }
}

function chooseArrearsDeal(obj) {
    $("#arrearsAmount").val($(obj).parents('.arrearsDeal').find('.arrears').html());
    $("#chooseDealArrears").html($(obj).parents('.arrearsDeal').find('.arrears').html());
}

openingDetails = function (operationTime, outpatientName) {
    var customerId = $("#customerId").val();
    var reservationsId = $("#reservationsId").val();
    var index = layer.open({
        type: 2,
        title: '开单详情',
        area: ['85%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/consultant/openingDetails/' + operationTime + '/' + customerId + '/' + reservationsId + '/' + outpatientName
    });
    this.layerIndex = index;
};

detailsPrint = function (operationTime,outpatientName) {
    var reservationsId = $("#reservationsId").val();
    var index = layer.open({
        type: 2,
        title: '打印详情',
        area: ['70%', '80%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/consultant/to_printP/' + operationTime  + '/' + reservationsId + '/' + outpatientName
    });
    this.layerIndex = index;
};

printingDeal = function (operationTime, outpatientName,getTime,reservationsId) {
    var reservationsId = $("#reservationsId").val();
    var index = layer.open({
        type: 2,
        title: '开单详情',
        area: ['85%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/consultant/to_prints/' + operationTime + '/'  + reservationsId + '/' + outpatientName + '/' + getTime
    });
    this.layerIndex = index;
};